﻿using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace SpriteGame.Helpers
{
    public enum CurrentMouseState
    {
        Idle,
        Dragging
    }

    class InputHandler
    {
        static Game1 parent;

        static GamePadState pad1State;
        static GamePadState pad1LastState = new GamePadState(); 

        static KeyboardState currentState;
        static KeyboardState lastState = new KeyboardState();

        static MouseState mouseState;
        static MouseState lastMouseState = new MouseState();

        static Vector2 lastMousePosition = Vector2.Zero;

        static Vector2 mousePosition;
        public static Vector2 MousePosition
        {
            get { return mousePosition; }
        }

        static Vector2 deltaMouse;
        /// <summary>
        /// Screenspace delta movement
        /// </summary>
        public static Vector2 DeltaMouse{
            get { return deltaMouse; }
        }

        static CurrentMouseState currentMouseState;
        public static CurrentMouseState MouseState
        {
            get { return currentMouseState; }
        }

        public static void LoadInputHandler(Game1 _parent)
        {
            parent = _parent;
        }

        public static bool LeftClick()
        {
            if (mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released)
            {
                return true;
            }
            return false;
        }

        public static bool LeftHold()
        {
            if (mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public static bool LeftRelease()
        {
            if (mouseState.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public static bool RightClick()
        {
            if (mouseState.RightButton == ButtonState.Pressed && lastMouseState.RightButton == ButtonState.Released)
            {
                return true;
            }
            return false;
        }

        public static bool RightHold()
        {
            if (mouseState.RightButton == ButtonState.Pressed && lastMouseState.RightButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public static bool RightRelease()
        {
            if (mouseState.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public static bool MiddleClick()
        {
            if (mouseState.MiddleButton == ButtonState.Pressed && lastMouseState.MiddleButton == ButtonState.Released)
            {
                return true;
            }
            return false;
        }

        public static bool MiddleHold()
        {
            if (mouseState.MiddleButton == ButtonState.Pressed && lastMouseState.MiddleButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public static bool MiddleRelease()
        {
            if (mouseState.MiddleButton == ButtonState.Released && lastMouseState.MiddleButton == ButtonState.Pressed)
            {
                return true;
            }
            return false;
        }

        public static Point GetMousePoint()
        {
            return new Point(mouseState.X, mouseState.Y);
        }
        
        public static void UpdateInput()
        {
            lastState = currentState;
            currentState = Keyboard.GetState();
            lastMouseState = mouseState;
            mouseState = Mouse.GetState();

            pad1LastState = pad1State;
            pad1State = GamePad.GetState(PlayerIndex.One);
            

            lastMousePosition = mousePosition;
            mousePosition = new Vector2(mouseState.X, mouseState.Y);

            deltaMouse = mousePosition - lastMousePosition;

            if((mouseState.X != lastMouseState.X) || (mouseState.Y != lastMouseState.Y))
            {
                currentMouseState = CurrentMouseState.Dragging;
            }
            else
            {
                currentMouseState = CurrentMouseState.Idle;
            }
        }

        public static MouseState CurrentState()
        {
            return mouseState;
        }

        public static Vector2 GetMousePositionWorldCoords()
        {
            return parent.ConvertScreenToWorld(mouseState.X, mouseState.Y);
        }

        public static Vector2 GetMousePositionScreenCoords()
        {
            return new Vector2(mouseState.X, mouseState.Y);
        }

        public static bool IsGamepadButtonPressed(Buttons _button)
        {
            if (pad1State.IsButtonDown(_button) && !pad1LastState.IsButtonDown(_button))
            {
                return true;
            }
            return false;
        }

        public static GamePadDPad GetDPadState()
        {
            return pad1State.DPad;
        }

        public static bool IsGamepadButtonReleased(Buttons _button)
        {
            if (pad1State.IsButtonUp(_button) && !pad1LastState.IsButtonDown(_button))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Gets a vector representation of the current gamepad thumbstick
        /// </summary>
        /// <param name="_leftRight">True for left false for right</param>
        /// <returns></returns>
        public static Vector2 GetThumbstickVector(bool _left)
        {
            Vector2 stickVec = Vector2.Zero;
            if(_left)
            {
                stickVec = new Vector2(pad1State.ThumbSticks.Left.X, pad1State.ThumbSticks.Left.Y);
            }
            else
            {
                stickVec = new Vector2(pad1State.ThumbSticks.Right.X, pad1State.ThumbSticks.Right.Y);
            }
            return stickVec;
        }

        public static bool IsGamepadButtonHeld(Buttons _button)
        {
            if (pad1State.IsButtonDown(_button) && pad1LastState.IsButtonDown(_button))
            {
                return true;
            }
            return false;
        }

        public static bool IsKeyPressed(Keys _key)
        {
            if (currentState.IsKeyDown(_key) && lastState.IsKeyUp(_key))
            {
                return true;
            }
            return false;
        }

        public static bool IsKeyHeldDown(Keys _key)
        {
            if (currentState.IsKeyDown(_key) && lastState.IsKeyDown(_key))
            {
                return true;
            }
            return false;
        }

        public static bool IsKeyReleased(Keys _key)
        {
            if (currentState.IsKeyUp(_key) && lastState.IsKeyDown(_key))
            {
                return true;
            }
            return false;
        }
    }
}
